

*******************************************************************************
*Want to create two files. 1) non-time varying characteristics 2) time varying characteristics


* create and format static characteristics
			//create temporary file for Army
use snpsht pid_pde date_birth_pde mil_lngv_pay_svc_base pn_sex race_cd afqt_p afqt_cat using "$data_in/Army_Master", replace
		gen base_date = dofc(mil_lngv_pay_svc_base)
		bys pid_pde (snpsht): egen temp_base = min(snpsht)
			replace base_date = temp_base if mi(base_date)
		bys pid_pde (snpsht): keep if _n == _N
		gen birthday = dofc(date_birth_pde)
		gen female = pn_sex == "F"
		keep pid_pde birthday base_date female race afqt_cat afqt_p
		format birthday base_date  %d
	tempfile Army
	save `Army'

	//create temporary file for other services
use snpsht pid_pde date_birth_pde mil_lngv_pay_svc_base pn_sex race_cd afqt_p afqt_cat using "$data_in/NonArmy_Master", clear
		gen base_date = dofc(mil_lngv_pay_svc_base)
		bys pid_pde (snpsht): egen temp_base = min(snpsht)
			replace base_date = temp_base if mi(base_date)
		bys pid_pde (snpsht): keep if _n == _N
		gen birthday = dofc(date_birth_pde)
		gen female = pn_sex == "F"
		keep pid_pde birthday base_date female race afqt_cat afqt_p
		format birthday base_date %d
	append using `Army'

	//only keep one instance if someone changes services
	bys pid_pde: keep if _n == _N

save "$data_out/demographics_nonvarying", replace




* create and format time-varying characteristics
use pid_pde snpsht service paygrade edu_lvl mrtl_stat pri_dod_occ prm_dty_stn_arrv afms_base_dt mil_lngv_pay_svc_yr_qy ppln_pgrd_ef using "$data_in/Army_Master", clear

gen quarter = qofd(dofc(snpsht))
format quarter %tq
sort pid_pde quarter
tempfile Army2
save `Army2' 

use  pid_pde snpsht service edu_lvl mrtl_stat pri_dod_occ prm_dty_stn_arrv afms_base_dt mil_lngv_pay_svc_yr_qy using "$data_in/NonArmy_Master", clear

gen quarter = qofd(dofc(snpsht))
format quarter %tq
sort pid_pde quarter


append using `Army2'

	//clean up file dropping missing SSN and reformatting dates
	drop if substr(pid,1,3) == "BAD"
	drop snpsht
	gen base_arrival = dofc(prm_dty_stn)
	gen begin_service = dofc(afms_base_dt)
	format base_arrival begin_service  %d
	rename mil_lngv longevity
	compress

	//only keep one instance if someone changes services in a given quarter
	bys pid_pde quarter (base_arrival): keep if _n ==1
save "$data_out/demographics_varying", replace


//identify rank at time of visit - this will also be used for promotions/demotions

use  pid_pde  paygrade ppln_pgrd_eff snpsht using "$data_in/Army_Master", replace
bys pid_pde paygrade ppln_pgrd_eff_dt (snpsht): keep if _n ==1
replace ppln = dofc(ppln)
format ppln %d
tempfile a
save `a'

use  pid_pde  paygrade ppln_pgrd_eff snpsht using "$data_in/NonArmy_Master", replace
bys pid_pde paygrade ppln_pgrd_eff_dt (snpsht): keep if _n ==1
replace ppln = dofc(ppln)
format ppln %d
append using `a'
drop if substr(pid_pde,1,3) == "BAD"
bys pid_pde (ppln): gen temp_n = _n
replace snpsht = dofc(snpsht)
format snpsht %d
rename (paygrade_pde ppln_pgrd)  (rank dor)
*if same date of rank is listed twice, keep more recent observation. <1% repeated date of rank and usually for E1 and E2
bys pid_pde dor (snpsht): keep if _n == _N

keep dor rank pid_pde
bys pid (dor): gen temp = _n
reshape wide rank dor, i(pid_pde) j(temp)

save "$data_out/rank", replace


//Identify discharge dates

*First identify last quarterly observation for each individual

use snpsht pid_pde  using "$data_in/Army_Master", replace

gen observation_date = dofc(snpsht_dt)
format observation_date %d

bys pid_pde: egen last_observed = max(observation_date)
format last_observed %d
bys pid_pde: keep if _n == 1

save "$data_out/temp_army_discharge", replace

use snpsht pid_pde  using "$data_in/NonArmy_Master", replace

gen observation_date = dofc(snpsht_dt)
format observation_date %d

bys pid_pde: egen last_observed = max(observation_date)
format last_observed %d
bys pid_pde: keep if _n == 1

save "$data_out/temp_nonarmy_discharge", replace
append using "$data_out/temp_army_discharge"
bys pid_pde (last_observed): keep if _n == _N
save "$data_out/discharge", replace


* Next identify precise date using transaction file
use pid_pde char txn_eff isvc using "$data_in/Discharges", clear
drop if substr(pid,1,3) == "BAD"
replace tx = dofc(txn)
format tx %d
bys pid_pde (txn): keep if _n == _N
rename char_svc_cd char
rename txn separation_date
sort pid_pde
merge m:1 pid_pde using "$data_out/discharge", keep(2 3)

gen ever_discharge = last_observed < d(31dec2017)
gen discharge_date = separation_date if ever_discharge == 1
format discharge_date %d

save "$data_out/discharge_sample", replace




